﻿package framework.core;

public class Condition 
{
	private String m_sql ;
	private Values m_params = new Values();
	
	private void init(String sql,Values params)
	{
		m_sql = sql ;
		if(params!=null) 
		{
			m_params = params ;
		}
	}

	public Condition(String sql,Value v)
	{
		Values values = new Values();
		values.add(v);
		init(sql,values);
	}
	public Condition(String sql,int v)
	{
		this(sql,new Value(v));
	}
	public Condition(String sql,double v)
	{
		this(sql,new Value(v));
	}
	public Condition(String sql,String v)
	{
		this(sql,new Value(v));
	}
	
	public Condition(String sql,int[] v)
	{
		Values values = new Values();
		if(v!=null)
		{
			for(int i=0;i<v.length;i++)
			{
				values.add(new Value(v[i]));
			}
		}
		init(sql,values);
	}
	public Condition(String sql,double[] v)
	{
		Values values = new Values();
		if(v!=null)
		{
			for(int i=0;i<v.length;i++)
			{
				values.add(new Value(v[i]));
			}
		}
		init(sql,values);
	}
	public Condition(String sql,String[] v)
	{
		Values values = new Values();
		if(v!=null)
		{
			for(int i=0;i<v.length;i++)
			{
				values.add(new Value(v[i]));
			}
		}
		init(sql,values);
	}
	public Condition(String sql,Values v)
	{
		init(sql,v);
	}
	public Condition(String sql)
	{
		init(sql,null);
	}
	public String getSql()
	{
		return m_sql ;
	}
	public int getParamCount()
	{
		return m_params.size();
	}
	public Value getParam(int index) 
	{
		return m_params.get(index);
	}
	
	public void and(Condition condition)
	{
		m_sql = "(" + m_sql ;
		m_sql += " and " ;
		m_sql += condition.getSql();
		m_sql += ")" ;
		for(int i=0;i<condition.getParamCount();i++)
		{
			m_params.add(condition.getParam(i));
		}
	}
	public void or(Condition condition)
	{
		m_sql = "(" + m_sql ;
		m_sql += " or " ;
		m_sql += condition.getSql();
		m_sql += ")" ;
		for(int i=0;i<condition.getParamCount();i++)
		{
			m_params.add(condition.getParam(i));
		}
	}
}
